<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.stuNameOrNo"
                        placeholder="学生姓名/学生学号"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Select v-model="params.classId" @on-change="changeSelect" placeholder="班级" style="width:100px" clearable>
                        <Option v-for="(item,index) in classList" :key="index" :value="item.classId">
                            {{item.className}}
                        </Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleScore">教师评分</Button>
                <Button style="margin-left: 10px;" @click="handleWeight">权重设置</Button>
                <Button style="margin-left: 10px;" @click="handleScoreExport">导出</Button>
            </div>
        </div>
        <Table :data="list" :columns="columns">
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button type="text" @click="handleDetail(row.stuId)">查看详情</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
        <div v-if="weightVisible">
            <WeightSetting
                :data="wData"
                @setWeight="setWeight"
                @closeWeight="closeWeight"
                v-model="weightVisible"
            />
        </div>
        <div v-if="scoreVisible">
            <TeacherScore
                :data="list"
                @ok="setTeacher"
                @cancel="closeTeacher"
                v-model="scoreVisible"
            />
        </div>
    </div>
</template>
<script>
import WeightSetting from './components/weight-setting'
import TeacherScore from './components/teacher-score'
import ScoreInfo from './components/score-info'
import { scoreList, updateWeight, getWeight, teaPick } from '@/api/score'
import { queryClassList } from '@/api/classManage'
import { download } from '@/api/fileManage'
export default {
    components: {
        WeightSetting,
        TeacherScore,
        // eslint-disable-next-line vue/no-unused-components
        ScoreInfo
    },
    inject: ['nextPageReload'],
    data () {
        return {
            wData: {},
            classList: [],
            weightVisible: false,
            scoreVisible: false,
            params: {
                classId: '',
                pageNum: 1,
                pageSize: 10,
                stuNameOrNo: ''
            },
            list: [],
            columns: Object.freeze([
                {
                    type: 'expand',
                    width: 30,
                    render: (h, params) => {
                        return h(ScoreInfo, {
                            props: {
                                value: params.row
                            }
                        })
                    }
                },
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '学生姓名', key: 'realName', align: 'center' },
                { title: '学生学号', key: 'userNo', align: 'center' },
                { title: '考试得分', key: 'examScore', align: 'center' },
                { title: '实训得分', key: 'trainingScore', align: 'center' },
                { title: '教师打分', key: 'teacherScore', align: 'center' },
                { title: '综合成绩', key: 'totalScore', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 100 }
            ])
        }
    },
    created () {
        this.getClassList()

        // this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        closeWeight () {
            this.weightVisible = false
        },
        setWeight (v) {
            let params = Object.assign(v, { classId: this.params.classId })
            updateWeight(params).then(res => {

            })
        },
        changeSelect (data) {
            console.log(data)
            this.params.classId = data
            this.queryList()
        },
        getClassList () {
            let params = {
                pageNum: 1,
                pageSize: 999
            }
            queryClassList(params).then(res => {
                if (res.data.code === '0000') {
                    this.classList = res.data.data.list
                    console.log(this.classList)
                    this.params.classId = this.classList[0].classId
                    this.queryList()
                }
            })
        },
        getWeightList () {
            let params = { classId: this.params.classId }
            getWeight(params).then(res => {
                console.log(res)
                let list = res.data.data
                // let types = {};
                list.map(item => {
                    this.wData[item.type] = item.weight
                })
                this.weightVisible = true
                console.log('权限')
                console.log(this.wData)
            })
        },
        queryList () {
            let params = {
                classId: this.params.classId,
                pageNum: this.params.pageNum,
                pageSize: this.params.pageSize,
                stuNameOrNo: this.params.stuNameOrNo
            }
            scoreList(params).then(res => {
                if (res.data.code != '0000') {
                    this.$Message.error(res.data.data)
                    return
                }
                this.list = res.data.data.list
                // 更新权重
                // this.getWeightList();
            })
        },
        handleScore () {
            this.scoreVisible = true
        },
        setTeacher (data) {
            // 保存
            let cjData = data.map(item => {
                return {
                    stuId: item.stuId,
                    teacherScore: item.teaScoreBefore
                }
            })
            teaPick(cjData).then(res => {
                if (res.data.code != '0000') {
                    this.$Message.error(res.data.data)
                    return
                }
                this.$Message.success('保存成功')
                this.queryList()
            })
        },
        closeTeacher () {
            this.scoreVisible = false
        },
        handleWeight () {
            this.getWeightList()
        },
        handleScoreExport () {
            download({ classId: this.params.classId }, '/gradeManage/exportDetail')
        },
        clearSearch () {},
        handleDetail (id) {
            this.$router.push({
                name: 'scoreDetail',
                query: {
                    id
                }
            })
            this.nextPageReload()
        }
    }
}
</script>
